package com.sun.future;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * @Author: Suncl
 * @Date: 2023/4/14 9:29
 */
public class CompletableFuture01 {

    public static void main(String[] args) throws Exception {
        ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);

        //runAsync 无返回值
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        });

        System.out.println("future1 硬拿返回值：" + future1.get());

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("future2 " + Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "succ";
        }, threadPool);
        System.out.println("future2 硬拿返回值：" + future2.get());


        try {
            CompletableFuture.supplyAsync(() -> {
                System.out.println("线程1 " + Thread.currentThread().getName() + "正在执行");
//                int i = 1/0;
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "suc";

            }, threadPool).whenComplete((result, ex) -> {
                System.out.println("返回值result=" + result);
                System.out.println("异常信息 e" + ex);
                if (ex == null) {
                    System.out.println("计算完成，获得返回值 " + result);
                }
            }).exceptionally(e -> {
                e.printStackTrace();
                System.out.println("异常信息 e" + e.getMessage());
                return null;
            });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
            System.out.println("线程池关闭了");
        }
        System.out.println("在外部执行的任务");

    }

    @Test
    public void test01() {
        ExecutorService threadPool = Executors.newScheduledThreadPool(10);
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "线程名称11");
            return "睡够了";
        }, threadPool).thenApplyAsync(v -> {
            System.out.println(Thread.currentThread().getName() + "线程名称12");

            System.out.println("thenApply入参" + v);
            return modelA(v);
        },threadPool).whenCompleteAsync((rel, ex) -> {
            System.out.println(Thread.currentThread().getName() + "线程名称13");

            if (ex == null) {
                System.out.println("返回结果:" + rel);
            }
        },threadPool).exceptionally(e -> {
            e.printStackTrace();
            return null;
        });


        System.out.println("最终结果" + completableFuture.join());
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束了");
    }

    private String modelA(String rel) {
        return rel + "modelA";
    }

}
